const { model, Sequelize } = require('../../core/model.js');

const { classesModel } = require('./classes')
const { usersModel } = require('./users')
const { answersModel } = require('./answers')

const papersModel = model.define('papers', {
    title: {
        type: Sequelize.STRING,
        validate:{
            notEmpty:true
        }
    },
    content: {
        type: Sequelize.TEXT,
        validate:{
            notEmpty:true
        }
    },
    item: {
        type: Sequelize.TEXT,
        validate:{
            notEmpty:true
        }
    },
    status: {
        type: Sequelize.ENUM('1', '0'),//1：关闭；0：开启；
        defaultValue: '1'
    },
    class_id: {
        type: Sequelize.INTEGER,
        validate:{
            notEmpty:true
        }
    },
    creator_id: {
        type: Sequelize.INTEGER,
        validate:{
            notEmpty:true
        }
    },

    ctime: Sequelize.DATE,
    mtime: Sequelize.DATE,
}, {
        createdAt: 'ctime',
        updatedAt: 'mtime',
        underscored: true
    })

papersModel.sync({
    // force: true
})


papersModel.hasMany(classesModel, {
    foreignKey: 'id',
    sourceKey:'class_id'
});
papersModel.hasMany(usersModel, {
    foreignKey: 'id',
    sourceKey: 'creator_id'
});

papersModel.hasMany(answersModel, {
    foreignKey: 'paper_id',
    // sourceKey: 'id'
});

module.exports = {
    papersModel,
    model,
    Op: Sequelize.Op
};